package com.atguigu.marketing.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.atguigu.marketing.bean.*;
import com.atguigu.marketing.mapper.MarketingRuleMapper;
import com.atguigu.marketing.service.MarketingRuleChannelService;
import com.atguigu.marketing.service.MarketingRuleGroupService;
import com.atguigu.marketing.service.MarketingRuleService;
import com.atguigu.marketing.service.MarketingRuleTimeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 营销规则 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2023-02-02
 */
@Service
@DS("marketing")
public class MarketingRuleServiceImpl extends ServiceImpl<MarketingRuleMapper, MarketingRule> implements MarketingRuleService {


    @Autowired
    MarketingRuleTimeService marketingRuleTimeService;

    @Autowired
    MarketingRuleGroupService marketingRuleGroupService;

    @Autowired
    MarketingRuleChannelService marketingRuleChannelService;

     public List<MarketingRule> getMarketingRuleList() {

         return this.baseMapper.selectAllMarketingRuleList();
     }

     public void saveMarketingRule(MarketingRule marketingRule){
         if(marketingRule.getId()==null){
             marketingRule.setCreateTime(new Date());
         }else{
             marketingRule.setUpdateTime(new Date());
         }
         saveOrUpdate(marketingRule);

         marketingRule.getMarketingRuleTime().setMarketingRuleId(marketingRule.getId());
         marketingRule.getMarketingRuleTime().setCreateTime(marketingRule.getCreateTime());
         marketingRule.getMarketingRuleTime().setUpdateTime(marketingRule.getUpdateTime());
         marketingRuleTimeService.saveOrUpdate(marketingRule.getMarketingRuleTime());

         marketingRule.getMarketingRuleGroup().setMarketingRuleId(marketingRule.getId());
         marketingRule.getMarketingRuleGroup().setCreateTime(marketingRule.getCreateTime());
         marketingRule.getMarketingRuleGroup().setUpdateTime(marketingRule.getUpdateTime());

//         List<String> groupIds = marketingRule.getMarketingRuleGroup().getGroupIds();
//         marketingRule.getMarketingRuleGroup().setGroupIdsJson(JSON.toJSONString(groupIds));



//         List<MarketingRuleUserAction> userActionTodayList = marketingRule.getMarketingRuleGroup().getUserActionTodayList();
//         marketingRule.getMarketingRuleGroup().setUserActionsTodayJson(JSON.toJSONString(userActionTodayList));


         marketingRuleGroupService.saveOrUpdate(marketingRule.getMarketingRuleGroup());

         marketingRule.getMarketingRuleChannel().setMarketingRuleId(marketingRule.getId());
         marketingRule.getMarketingRuleChannel().setCreateTime(marketingRule.getCreateTime());
         marketingRule.getMarketingRuleChannel().setUpdateTime(marketingRule.getUpdateTime());
         marketingRuleChannelService.saveOrUpdate(marketingRule.getMarketingRuleChannel());

     }

    @Override
    public MarketingRule getMarketingRule(String id) {
        MarketingRule marketingRule = getById(id);
        MarketingRuleTime marketingRuleTime = marketingRuleTimeService.getOne(new QueryWrapper<MarketingRuleTime>().eq("marketing_rule_id", id));
        marketingRule.setMarketingRuleTime(marketingRuleTime);
        MarketingRuleGroup marketingRuleGroup = marketingRuleGroupService.getOne(new QueryWrapper<MarketingRuleGroup>().eq("marketing_rule_id",id));
        marketingRule.setMarketingRuleGroup(marketingRuleGroup);
        MarketingRuleChannel marketingRuleChannel = marketingRuleChannelService.getOne(new QueryWrapper<MarketingRuleChannel>().eq("marketing_rule_id",id));
        marketingRule.setMarketingRuleChannel(marketingRuleChannel);
        return marketingRule;
    }
}
